// index.js
Page({
  data: {
    centerY:30.302915,
    centerX:120.081699,
    scale:'14.2',
    showDialog: false,
    favor: false,
    markers: [],
    mapId: "myMap" ,
    minscale: '12.5',
    satelliteMode: false,
    homeFindMode: false,
    showTools: true,
    showFilterPopup: false, // 控制筛选弹窗的显示与隐藏
    selectedOption: '', // 选中的筛选条件
    onPressAdder: false,
    filterOptions: ['cat', 'dog', 'bird', 'insect'] ,// 筛选条件列表
    polyline: [{
      points: [
        { longitude: 120.06925648269743, latitude: 30.305400533419454 },
        { longitude: 120.06984394046062, latitude: 30.303818247901035 },
        { longitude: 120.06994394046062, latitude: 30.299918247901035 },
        { longitude: 120.07009394046062, latitude: 30.296618247901035 },
        { longitude: 120.07123343562603, latitude: 30.296883861399985 },
        { longitude: 120.07223343562603, latitude: 30.296753861399985 },
        { longitude: 120.07443343562603, latitude: 30.295713861399985 },
        { longitude: 120.07723343562603, latitude: 30.295183861399985 },
        { longitude: 120.08125784431073, latitude: 30.294921496288748 },
        { longitude: 120.09272191710808, latitude: 30.29502041605682 },
        { longitude: 120.09242191710808, latitude: 30.305588247901035 },
        { longitude: 120.09102191710808, latitude: 30.315588247901035 },
        { longitude: 120.06925648269743, latitude: 30.305400533419454 }
      ],
      color: "#FF101D",
      width: 4,
      dottedLine: false
    }]
  },
  
  onReady: function () {
    this.mapCtx = wx.createMapContext('myMap')
    var app = getApp();
    setTimeout(() => {
      this.setData({
        markers: app.globalData.markers,
      })
    }, 500);

  },

  onLoad: function () {
    console.log("Map Loading successfully!")
  },

  refresh() {
    var app = getApp();
    app.getMarkers()
    .then((markers) => {
      // 标记点数据获取成功后更新页面数据
      this.data.markers = markers
      this.setData({
        showTools: true,
        showFilterPopup: false,
        selectedOption: '',
        markers: this.data.markers
      })
      
    })
    .catch((error) => {
      console.error(error);
    });
  },

  toggleTools() {
    this.setData({ 
      showTools: !this.data.showTools
    });
  },

  pressAdder() {
    this.setData({ 
      onPressAdder: true
    });
  },
  
  leaveAdder() {
    this.setData({ 
      onPressAdder: false
    });
  },


  toggleFilterPopup() {
    var app = getApp();
    this.setData({
      showFilterPopup: !this.data.showFilterPopup
    });
  },

  selectFilterOption(event) {
    let that = this;
    var app = getApp();
    this.setData({
      markers: app.globalData.markers
    });
    const selectedOption = event.currentTarget.dataset.option;
    let tempmarkers = [];
    let index = 0;
    this.setData({
      selectedOption,
      showFilterPopup: false
    });
    console.log('选中的筛选条件:', selectedOption);
    for(let item of that.data.markers){      
      if(item.label.race!==selectedOption){
        index++;
      }
      else {
        let newItem = Object.assign({}, item);
        newItem.id -= index;
        tempmarkers.push(newItem);
      }
    }
    this.setData({
      markers:tempmarkers,
    })
  },

  clearFilter() {
    var app = getApp();
    this.setData({
      markers: app.globalData.markers,
      selectedOption: '',
      showFilterPopup: false,
    });
  },

  ToAnimalDetail: function() {
    var app = getApp();
    let id = this.data.markers[app.globalData.currentMarker - 1].label.animal_id;
    wx.navigateTo({
      url: '/pages/animalDetail/animalDetail?AnimalId=' + id,
    })
  },

  ToPostDetail: function() {
    var app = getApp();
    var id = app.globalData.currentMarkerToPost;
    wx.request({
      url: 'https://api.fracher21.top/spotit/getuserinfo',
      method: "GET",
      data: {
        token: app.globalData.token   
      },
      success: (res) => {
        wx.navigateTo({
          url: '/pages/posts/postDetail/postDetail?post_id=' + id + '&username=' + res.data.name + '&userid=' + res.data.id,
        })
      }
    })
  },
  
  handleFavor:function(){
    var app = getApp();
    wx.request({
      url: this.data.favor? 'https://api.fracher21.top/spotit/unstarpost' : 'https://api.fracher21.top/spotit/starpost',
      method: "POST",
      data: {
        token: app.globalData.token,
        id: app.globalData.currentMarkerToPost, 
      },
      success: (res) => {
        this.setData({
          favor: !this.data.favor,
        })
        this.data.markers[app.globalData.currentMarker - 1].label.favor = this.data.favor;
      }
    })
  },

  moveTolocation: function () {
    let Id = this.data.mapId
    var mapContext = wx.createMapContext(Id);
    const latitude = 30.302915; 
    const longitude = 120.081699; 
    this.setData({
      homeFindMode: true,
    })
    mapContext.moveToLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 14.2,
    });
    setTimeout(() => {
      this.setData({
        homeFindMode: false,
      })
    }, 500);
  },


  // 点击标点获取数据
  markertap(e) {
    var app = getApp();
    var id = e.markerId;
    var name = this.data.markers[id - 1].name
    var description = this.data.markers[id - 1].label.desc
    var location = this.data.markers[id - 1].label.locdesc
    var head = this.data.markers[id - 1].label.head
    var markers = this.data.markers
    app.updatecurrentMarker(id);
    app.updatecurrentMarkerToPost(this.data.markers[id - 1].label.post_id);
    markers[id - 1].width = '45'; 
    markers[id - 1].height = '45'; 
    

    this.setData({
      favor: this.data.markers[id - 1].label.favor,
      AnimalName: name,
      AnimalDescription: description,
      AnimalLocation: location,
      AnimalHeadImg: head,
      showDialog: true,
      markers: markers,
    })
  },

  toggleDialog: function () {
    var app = getApp();
    var id = app.globalData.currentMarker;
    var markers=this.data.markers;
    markers[id - 1].width = '35'; 
    markers[id - 1].height = '35'; 
    this.setData({
      showDialog: false,
      markers: markers,
    })
  },

  toggleMapType: function() {
    this.setData({
      satelliteMode: !this.data.satelliteMode,
    });
  },

  onShow() {
    var app = getApp();
    console.log("show 123")
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }

    // 标记点数据获取成功后更新页面数据
    this.setData({
      showTools: true,
      showFilterPopup: false,
      selectedOption: ''
    })
    app.updateMarkers();
    // this.data.markers = markers



    this.clearFilter()
    console.log("show 234")
    
  },
})



